package com.hawthorn.admin.service.sysmessage.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.repository.SysMessageMapper;
import com.hawthorn.admin.service.sysmessage.SysMessageService;
import com.hawthorn.component.constant.DicConstant;
import com.hawthorn.component.constant.FormatConstant;
import com.hawthorn.entity.po.SysMessagePO;
import com.hawthorn.platform.context.ContextHolder;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.service.TransResultService;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.platform.utils.common.StrMyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 系统消息服务实现类 </p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/11/25 5:28 PM
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
public class SysMessageServiceImpl implements SysMessageService
{
  @Resource
  private SysMessageMapper sysMessageMapper;
  @Autowired
  private RedisMyClient redisMyClient;

  @Resource
  private TransResultService transResultService;

  private HashMap<String, String> getTransHash()
  {
    HashMap<String, String> transHash = new HashMap<>();
    transHash.put("type", "dic|xxlx");
    transHash.put("status", "dic|xxzt");
    transHash.put("deptId", "dept|o");
    transHash.put("createTime", "date|" + FormatConstant.SIMPLE_DATE_TIME_FORMAT);
    transHash.put("createBy", "user|o");

    return transHash;
  }

  @Override
  public IPage<Map<String, Object>> findAllMsg(Page<Map<String, Object>> page, String conditions)
  {
    String userAccount = ContextHolder.getUserAccount();
    // 设置排序
    OrderItem oi = new OrderItem();
    oi.setColumn("createTime");
    oi.setAsc(true);
    List<OrderItem> al = page.getOrders();
    al.add(oi);
    page.setOrders(al);

    // todo 获取redis中uer的oemCompanyId、deptId
    Map userInfo = redisMyClient.getUserInfo(userAccount);
    Long omeCompanyId = Long.valueOf((String) userInfo.get("oemCompanyId"));
    Long deptId = Long.valueOf((String) userInfo.get("deptId"));
    return transResultService.transResult(sysMessageMapper.findAllMsg(page, conditions, userAccount, deptId, omeCompanyId), getTransHash());
  }

  @Override
  public Integer findMsgOneWeekNums(String userAccount)
  {
    // todo 获取redis中uer的oemCompanyId、deptId
    Map userInfo = redisMyClient.getUserInfo(userAccount);
    String oemCompanyId = MyUtil.getValue(userInfo.get("oemCompanyId"));
    Long omeCompanyId = 1L;
    if (StrMyUtil.isNotEmpty(oemCompanyId))
    {
      omeCompanyId = Long.valueOf(oemCompanyId);
    }

    Long deptId = Long.valueOf((String) userInfo.get("deptId"));
    return sysMessageMapper.findMsgOneWeekNums(userAccount, omeCompanyId, deptId);
  }

  /**
   * <p>todo 首页获取一周内系统消息提醒</p>
   * @param: userId
   * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
   * @author: andy.ten@tom.com | xukaixing@hotmail.com
   * @date: 2021/12/12 12:46 PM
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/12/12    andy.ten        v1.0.1             init
   */
  @SuppressWarnings("rawtypes")
  @Override
  public List<Map<String, Object>> findMsgOneWeek(String userAccount)
  {
    // todo 获取redis中uer的oemCompanyId、deptId
    Map userInfo = redisMyClient.getUserInfo(userAccount);
    Long omeCompanyId = Long.valueOf((String) userInfo.get("oemCompanyId"));
    Long deptId = Long.valueOf((String) userInfo.get("deptId"));
    return sysMessageMapper.findMsgOneWeek(userAccount, omeCompanyId, deptId);
  }

  @Override
  public List<Map<String, Object>> findMsgOneWeek2(String userAccount)
  {
    // todo 获取redis中uer的oemCompanyId、deptId
    Map userInfo = redisMyClient.getUserInfo(userAccount);
    Long omeCompanyId = Long.valueOf((String) userInfo.get("oemCompanyId"));
    Long deptId = Long.valueOf((String) userInfo.get("deptId"));
    return sysMessageMapper.findMsgOneWeek2(userAccount, omeCompanyId, deptId);
  }

  /**
   * <p>todo 获取强制阅读消息</p>
   * @param: userAccount
   * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
   * @author: andy.ten@tom.com | xukaixing@hotmail.com
   * @date: 2021/12/17 10:00 AM
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/12/17    andy.ten        v1.0.1             init
   */
  @Override
  public List<Map<String, Object>> findMsgForceView(String userAccount)
  {
    // todo 获取redis中uer的oemCompanyId、deptId
    Map userInfo = redisMyClient.getUserInfo(userAccount);
    Long omeCompanyId = Long.valueOf((String) userInfo.get("oemCompanyId"));
    Long deptId = Long.valueOf((String) userInfo.get("deptId"));
    return sysMessageMapper.findMsgForceView(userAccount, omeCompanyId, deptId);
  }

  /**
   * <p>todo 设置消息状态 为："已完成"</p>
   * @param: msgId
   * @param: userAccount
   * @return: void
   * @author: andy.ten@tom.com | xukaixing@hotmail.com
   * @date: 2021/12/16 7:32 PM
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/12/16    andy.ten        v1.0.1             init
   */
  @Override
  public void updateMsgForFinish(Long msgId, String userAccount)
  {

    UpdateWrapper<SysMessagePO> uw = new UpdateWrapper<>();
    SysMessagePO sysMessagePO = sysMessageMapper.selectById(msgId);
    sysMessagePO.setStatus(Integer.valueOf(DicConstant.TXXXZT_03));
    sysMessagePO.setFinishAccount(userAccount);
    sysMessagePO.setFinishTime(LocalDateTime.now());
    uw.eq("id", msgId);
    // 采用该方式可以支持自动填充，如果设置为null，则只更新指定字段，但自动填充失效
    sysMessageMapper.update(sysMessagePO, uw);
  }
}
